#ifndef _DATAELTS_H_
#define _DATAELTS_H_




typedef struct {
	DataElement		elt;
//	unsigned char	presence;
/* Mandatory 1, Optional 0 */
//	unsigned char	state;
	/* 0 not initialised, 1 initialised */
//	unsigned char   mode;
    /* 0x1 internal access only */
							 /* ... */
//	unsigned char	format;
	/* 0x1	n	numeric*/
							 /* 0x2 cn	compressed numeric */
							 /* 0x3	b	binary */
//	unsigned char	Template;
	/* 0x0 no Template */
							 /* 0x1 Application Template */
							 /* 0x2 FCI Template */
							 /* ....*/

} DataElementExt;


typedef struct {
	unsigned int				dataEltsCount;
	unsigned int				MaxDataElt;
	DataElementExt	*DataElementTable;
	unsigned int				bufSize;
	unsigned int				MaxBufSize;
	unsigned char	*StorageBuffer;
}DB;



#define MAX_MAIN_ELT 160
#define MAX_MAIN_STORAGE 1024*16

#define MAX_PARAM_ELT 80
#define MAX_PARAM_STORAGE 1024*2


#define DB_OK				0
#define DB_BUILDDOLOK		DB_OK
#define DB_DATA_NOT_FOUND	-1
#define	DB_FULL				-2
#define	NO_DOL				-3
#define DB_KO				-4

/*& BEGIN Externalisation ODA 									*/
typedef struct {
	unsigned char	*TLV_data;
	unsigned int	TLV_len;
	unsigned int	TLV_idx;
} TLVBUF;

/* IsTAGKnown ? */
#define TAG_KNOWN							8
#define TAG_UNKNOWN							9
#define TAG_IGNORED							10

#define TLV_END			-1
#define TLV_ELT_READ	-2
#define TLV_TAG_READ	-3
#define TLV_LEN_READ	-4
#define TLV_ERROR		-5
#define TLV_VALUE_READ  -6

/* Format */
#define ALPHANUMERIC			1
#define ALPHANUMERIC_SPECIAL	2
#define BINARY					3
#define COMPRESSED_NUMERIC		4
#define NUMERIC					5

/*& END															*/

unsigned short CharToShort(unsigned char *);
void           ShortToChar(unsigned short,unsigned char *);
unsigned long  CharToLong(unsigned char *donnees);
void           LongToChar(unsigned long val,unsigned char *donnees);

int DB_Init(DB* aDB,DataElementExt *Table,unsigned char* Buf,unsigned int MaxDataElt,unsigned int MaxBufSize);

/* Get an element with its tag */
int DB_GetDataElementExt( DB* aDB,
						  unsigned long  tag,
						  DataElementExt *data);

int DB_GetElementIdx(DB* aDB,unsigned long  tag);

/* Store a Data Element */
int DB_StoreDataElement(DB* aDB,const DataElement* aData);

int DB_StoreDEL(DB* aDB,const DEL* aDEL);

#ifndef _DB_UTIL_NO_DUMP_
void DB_Dump(DB* aDB);
#endif

#define AAR		0xC0
#define ARQC	0x80
#define TC		0x40
#define AAC		0x00


int DB_Copy(DB* Source, DB* Target);

/*& BEGIN Externalisation ODA 									*/
/* Return the Format */
int DB_DataFormat(unsigned int tag);

int TLV_ParseInit(TLVBUF *TLVStruct, unsigned char *data, unsigned int len);

int TLV_ParseTag(TLVBUF *TLVStruct,unsigned long *tag);

int TLV_ParseLen(TLVBUF *TLVStruct,unsigned short *length);

int TLV_ParseValue(TLVBUF *TLVStruct,unsigned short len, unsigned char **value);

int TLV_Parse(TLVBUF *TLVStruct,DataElement *anElt);

/* Return if the tag is known or not */
int DB_IsTAGKnown(unsigned int tag);

int DB_IsNotConstructedTag(unsigned int tag);
/*& END 														*/

int DB_GetTagLength( unsigned long tag,
				  unsigned short *len,
				  unsigned short *min,
				  unsigned short *max
				);

#endif

